//
//  XNLoginViewController.m
//  XINNingOA
//
//  Created by 葛承铭 on 2021/7/11.
//

#import "XNLoginViewController.h"
#import "XNLoginRequest.h"
#import "XNHud.h"
#import "XNMainViewController.h"
#import "XNBaseNavigationViewController.h"
#import "SVProgressHUD.h"

@interface XNLoginViewController ()

@property (nonatomic, weak) UITextField *accountText;

@property (nonatomic, weak) UITextField *passwordText;

@end

@implementation XNLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self renderView];
    // Do any additional setup after loading the view.
}

- (void)renderView
{
    UIImageView *footerImage = [[UIImageView alloc] init];
    footerImage.image = [UIImage imageNamed:@"KD_login_footer"];
    [self.view addSubview:footerImage];
    [footerImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.mas_equalTo(170);
    }];
    
    UIImageView *headerImage = [[UIImageView alloc] init];
    headerImage.image = [UIImage imageNamed:@"KD_login_header"];
    [self.view addSubview:headerImage];
    [headerImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(84);
        make.left.equalTo(self.view).offset(38);
        make.size.mas_equalTo(CGSizeMake(158, 45));
    }];
    
    UILabel *loginLab = [[UILabel alloc] init];
    loginLab.text = @"登陆";
    loginLab.font = [UIFont systemFontOfSize:30 weight:UIFontWeightBold];
    [self.view addSubview:loginLab];
    [loginLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(headerImage);
        make.top.equalTo(headerImage).offset(27);
        make.size.mas_offset(CGSizeMake(100, 30));
    }];
    
    UIImageView *iconImage = [[UIImageView alloc] init];
    iconImage.image = [UIImage imageNamed:@"KD_login_icon"];
    [self.view addSubview:iconImage];
    [iconImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(96);
        make.right.equalTo(self.view).offset(-35);
        make.size.mas_equalTo(CGSizeMake(46, 46));
    }];
    
    
    UILabel *accountlab = [[UILabel alloc] init];
    accountlab.text = @"帐号";
    accountlab.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    accountlab.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:accountlab];
    [accountlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(40);
        make.top.equalTo(loginLab.mas_bottom).offset(44);
        make.height.mas_equalTo(16);
    }];
    
    
    UITextField *accountText = [[UITextField alloc] init];
    accountText.placeholder = @"请输入帐号";
    accountText.font = [UIFont systemFontOfSize:20 weight:UIFontWeightBold];
    [self.view addSubview:accountText];
    self.accountText = accountText;
    [accountText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(40);
        make.right.equalTo(self.view).offset(-37);
        make.top.equalTo(accountlab.mas_bottom).offset(20);
        make.height.mas_equalTo(22);
    }];
    
    UILabel *accountLine = [[UILabel alloc] init];
    accountLine.backgroundColor = [UIColor colorWithHexString:@"#C6D2DE"];
    [self.view addSubview:accountLine];
    [accountLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(40);
        make.right.equalTo(self.view).offset(-40);
        make.height.mas_equalTo(2);
        make.top.equalTo(accountText.mas_bottom).offset(8);
    }];
    
    UILabel *passwordlab = [[UILabel alloc] init];
    passwordlab.text = @"密码";
    passwordlab.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    passwordlab.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:passwordlab];
    [passwordlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(40);
        make.top.equalTo(accountLine.mas_bottom).offset(34);
        make.height.mas_equalTo(16);
    }];
    
    
    UITextField *passwordText = [[UITextField alloc] init];
    passwordText.placeholder = @"请输入密码";
    passwordText.secureTextEntry = YES;
    passwordText.font = [UIFont systemFontOfSize:20 weight:UIFontWeightBold];
    [self.view addSubview:passwordText];
    self.passwordText = passwordText;
    [passwordText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(40);
        make.right.equalTo(self.view).offset(-37);
        make.top.equalTo(passwordlab.mas_bottom).offset(20);
        make.height.mas_equalTo(22);
    }];
    
    UILabel *passwordLine = [[UILabel alloc] init];
    passwordLine.backgroundColor = [UIColor colorWithHexString:@"#C6D2DE"];
    [self.view addSubview:passwordLine];
    [passwordLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(40);
        make.right.equalTo(self.view).offset(-40);
        make.height.mas_equalTo(2);
        make.top.equalTo(passwordText.mas_bottom).offset(8);
    }];
    
    
//    UIView *checkView = [[UIView alloc] init];
//    [self.view addSubview:checkView];
//    [checkView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.view).offset(40);
//        make.top.equalTo(passwordLine.mas_bottom).offset(23);
//    }];
//
//    UIButton *checkbtn = [[UIButton alloc] init];
//    [checkbtn setImage:[UIImage imageNamed:@"KD_checkbox"] forState:UIControlStateNormal];
//    [checkbtn setImage:[UIImage imageNamed:@"KD_checkbox_selected"] forState:UIControlStateSelected];
//    [checkView addSubview:checkbtn];
//    [checkbtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.bottom.left.equalTo(checkView);
//        make.size.mas_equalTo(CGSizeMake(24, 24));
//    }];
    
//    UILabel *rememberLab = [[UILabel alloc] init];
//    rememberLab.text = @"记住密码";
//    rememberLab.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
//    [checkView addSubview:rememberLab];
//    [rememberLab mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(checkbtn.mas_right).offset(5);
//        make.centerY.equalTo(checkbtn);
//        make.height.mas_equalTo(14);
//        make.right.equalTo(checkView);
//    }];
//
//    UIButton *settingbtn = [[UIButton alloc] init];
//    [settingbtn setTitle:@"设置服务器" forState:UIControlStateNormal];
//    [settingbtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
//    [settingbtn addTarget:self action:@selector(setClick:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:settingbtn];
//    [settingbtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.width.mas_equalTo(100);
//        make.top.equalTo(passwordLine).offset(23);
//        make.height.mas_equalTo(22);
//        make.right.equalTo(self.view).offset(-40);
//    }];
    
    
    UIButton *loginBtn = [[UIButton alloc] init];
    [loginBtn setTitle:@"登 录" forState:UIControlStateNormal];
    loginBtn.backgroundColor = [UIColor colorWithHexString:@"#358AFF"];
    loginBtn.layer.cornerRadius = 5;
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginBtn addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginBtn];
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(38);
        make.right.equalTo(self.view).offset(-38);
        make.top.equalTo(passwordLine.mas_bottom).offset(30);
        make.height.mas_equalTo(48);
    }];
    

//    self.accountText.text = @"20212021";
//    self.passwordText.text = @"888888";
    
//    UITextView *tokenLab = [[UITextView alloc] init];
//    tokenLab.text = [KDUserAccount sharedInstance].deviceToken;
//    tokenLab.font = [UIFont systemFontOfSize:10];
//    [self.view addSubview:tokenLab];
//    [tokenLab mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.view).offset(10);
//        make.right.equalTo(self.view).offset(-10);
//        make.top.equalTo(loginBtn.mas_bottom).offset(10);
//        make.height.mas_equalTo(20);
//    }];

    
}


- (void)login:(UIButton *)sender
{
    [self.view endEditing:YES];
    [SVProgressHUD show];
    XNLoginRequest *request = [[XNLoginRequest alloc] initWithUsername:self.accountText.text Password:self.passwordText.text];
    [request startWithCompletionBlockWithSuccess:^(__kindof XNLoginRequest * _Nonnull request) {
        [SVProgressHUD dismiss];
        if (request.success) {
            [[NSUserDefaults standardUserDefaults] setObject:self.accountText.text forKey:@"account"];
            [[NSUserDefaults standardUserDefaults] setObject:self.passwordText.text forKey:@"password"];
            XNBaseNavigationViewController *nav = [[XNBaseNavigationViewController alloc] initWithRootViewController:[[XNMainViewController alloc] init]];
            [UIApplication sharedApplication].keyWindow.rootViewController = nav;
        }else{
            [XNHud showToastWithText:@"账号密码错误"];
        }

    } failure:^(__kindof XNLoginRequest * _Nonnull request) {
        [SVProgressHUD dismiss];
    }];



}

@end
